//
//  FindViewController.m
//  Four_Fit
//
//  Created by xalo on 16/7/12.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "FindViewController.h"
#import "FindModel.h"
#import "UrlManager.h"
#import "RecTableModel.h"
#import "RecScrollModel.h"
#import "RecommendCell.h"
#import "ScrollManager.h"
#import "EatModel.h"
@interface FindViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *recommend;
@property (weak, nonatomic) IBOutlet UIButton *inspirational;
@property (weak, nonatomic) IBOutlet UIButton *eating;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UIView *underlineView;

@property (nonatomic,strong)UIView *signView;
@property (nonatomic,assign)BOOL animation;


@property (nonatomic,strong)UIScrollView *recommScrollview;
@property (nonatomic,strong)NSMutableArray *recommendArray;
@property (weak, nonatomic) IBOutlet UITableView *recommTableview;
@property (nonatomic,strong)NSMutableArray *recTabArray;

@property (nonatomic,strong)NSMutableArray *recScrArray;

@property (nonatomic,strong)NSMutableArray *urlArr;///照片网址数组


@property (weak, nonatomic) IBOutlet UITableView *eatTableView;





@end

@implementation FindViewController
- (NSMutableArray *)urlArr{
    if (!_urlArr) {
        _urlArr = [NSMutableArray array];
    }
    return _urlArr;
}
- (NSMutableArray *)recTabArray{
    if (!_recTabArray) {
        _recTabArray = [NSMutableArray array];
    }
    return _recTabArray;
}
- (NSMutableArray *)recommendArray{
    if (!_recommendArray) {
        _recommendArray = [NSMutableArray array];
    }
    return _recommendArray;
}

- (UIView *)signView{
    if (!_signView) {
        _signView = [[UIView alloc]init];
        _signView.size = CGSizeMake((kWidth-40)/3-20, 3);
        _signView.center = CGPointMake(self.recommend.center.x, 0);
        [self.underlineView addSubview:_signView];
        
    }
    return _signView;
    
}
- (UIScrollView *)recommScrollview{
    if (!_recommScrollview) {
        _recommScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/4)];
        _recommScrollview.contentSize = CGSizeMake(kWidth*self.recScrArray.count, kHeight/4);
        _recommScrollview.delegate = self;
        _recommScrollview.bounces = NO;
        _recommScrollview.bouncesZoom = NO;
        
    }
    return _recommScrollview;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.title = @"发现";
    [self.recommTableview registerNib:[UINib nibWithNibName:@"RecommendCell" bundle:nil] forCellReuseIdentifier:@"recommend_cell"];
    
    self.recommTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}
- (void)viewDidAppear:(BOOL)animated{
    self.signView.backgroundColor = [UIColor redColor];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    [[QZRequestManager sharedManager] fetchDataWithUrl:@"http://api.fit-time.cn/ftinfo/refreshInfoFeeds?page_size=20" parameters:@{} method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *smallDic in bigDic[@"infoFeeds"]) {
            FindModel *model = [[FindModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.recommendArray addObject:model];
        }
 
    } updateUI:^{
        ///轮播图数据请求
        [[QZRequestManager sharedManager] fetchDataWithUrl:@"http://api.fit-time.cn/ftinfo/listRecommends" parameters:@{} method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
            
            NSDictionary *scrDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSLog(@"%@",scrDic);
            for (NSDictionary *smallDic in scrDic[@"recommends"]) {
                RecScrollModel *model = [[RecScrollModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.recScrArray addObject:model];
                NSString *url = [@"http://ft-info.fit-time.cn/" stringByAppendingString:[model.photo substringFromIndex:8]];
                [self.urlArr addObject:url];
            }
            
        } updateUI:^{
            
            
             ///推荐列表数据请求
        NSString *url = [[UrlManager creatUrlManager]packageURLWithNsArray:self.recommendArray andUrl:@"http://api.fit-time.cn/ftinfo/getInfoByIds?stat=1&"];
        [[QZRequestManager sharedManager] fetchDataWithUrl:url parameters:@{} method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
            
            NSDictionary *tableDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            for (NSDictionary *smallDic in tableDic[@"infos"]) {
                RecTableModel *model = [[RecTableModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.recTabArray addObject:model];
            }
            
        } updateUI:^{
            self.recommTableview.tableHeaderView =[[ScrollManager alloc]initWithFrame:CGRectMake(0, 0, kWidth, 200) imageArray:self.urlArr];
            [self.recommTableview reloadData];
            
        }];
 
        }];
        
    }];
    
    
}
#pragma mark=============UITableViewDelegate================
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.recommTableview) {
        return self.recTabArray.count;
    }
    
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RecTableModel *model = self.recTabArray[indexPath.row];
    RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recommend_cell" forIndexPath:indexPath];
    [cell setCellWithModel:model];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==self.recommTableview) {
        return 200;
    }
    return 200;
}



#pragma mark==========ScrollViewDelegate设置===========
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGPoint offset = scrollView.contentOffset;
    CGFloat percent = offset.x/(kWidth*2);
    if (self.animation ) {
        [UIView animateWithDuration:0.5 animations:^{
            self.signView.centerX = self.recommend.centerX+(self.inspirational.centerX-self.recommend.centerX)*percent;
            self.animation = NO;
        }];
    }else{
        self.signView.centerX = self.recommend.centerX+(self.inspirational.centerX-self.recommend.centerX)*percent;
    }
  
    NSArray *visibleCells = [self.recommTableview visibleCells];
    for (RecommendCell *cell in visibleCells) {
        
        [cell cellOnTableView:self.recommTableview didScrollOnView:self.view];
    }
    
    
    
}
- (IBAction)recommendAction:(UIButton *)sender {
    self.animation = YES;
    self.scrollview.contentOffset = CGPointMake(0, 0);
    
    
}
- (IBAction)eatingAction:(UIButton *)sender {
    self.animation = YES;
    self.scrollview.contentOffset = CGPointMake(kWidth, 0);
}
- (IBAction)inspirational:(UIButton *)sender {
    self.animation = YES;
    self.scrollview.contentOffset = CGPointMake(kWidth*2, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
